exports.CreateDownloadPop = function(_args) {
    if(_args === undefined) _args = {};
    if(_args.title === undefined) _args.title = 'Popover';

	Ti.App.Properties.setString('login_username_email', '');
	Ti.App.Properties.setString('login_password', '');
		
		
    var win = Ti.UI.createWindow({
    	top:0,
    	left:Titanium.Platform.displayCaps.platformWidth,
        width: Titanium.Platform.displayCaps.platformWidth,//320,
        height: Titanium.Platform.displayCaps.platformHeight,//480,
        opacity: 0,
        backgroundColor:'transparent'
    });
    var btwidth=200;
	var btheight=45;
    var backgroundImage,arrowimage;
    var topfirst,leftfirst,leftsecond,heightlabel,right,heighttext,space,bgcolor='#020202'	
	var aps='';
    if(Ti.Platform.osname== 'android') {
  		 backgroundImage= '../windows/images/popover/PopoverPortrait.png';
  		 arrowimage='../windows/images/popover/PopoverPortraitArrow.png';
  		 Titanium.include('../globals.js');
  		 Titanium.include('../cs/cs.js');
  		 Titanium.include('../windows/ModalActivityIndicatorWindow.js');
  		 aps='../';
  		 topfirst=30,leftfirst=40,leftsecond=200,heightlabel=21,right=40,heighttext=50,space=10
    }
    else {
   		  backgroundImage= 'windows/images/popover/PopoverPortrait.png';
   		  arrowimage='windows/images/popover/PopoverPortraitArrow.png';
   		  Titanium.include('globals.js');
   		  topfirst=30,leftfirst=40,leftsecond=200,heightlabel=21,right=40,heighttext=31,space=10
	
    }
    
    
    
    var fakeWin = Ti.UI.createView({
        width: Titanium.Platform.displayCaps.platformWidth,//300,
        height: Titanium.Platform.displayCaps.platformHeight,//350,
        top:0,// 65,
        backgroundColor:'transparent'
        //backgroundImage:Globals.backgroundImage_PanelHeaderBuyDetail,
    });
    win.add(fakeWin);

    win.addEventListener('close', function() {
        win = null;
    });

    win.addEventListener('click', function(e) {
        //alert(e.source);
        if(e.source === win) {
            win.close();
        }
    });

    var view = Ti.UI.createView({
        width:505,//,400
        height:290,//,418
        //top: 40,
        backgroundColor: 'transparent',
        backgroundImage:aps+'images/downloadfile.png',
        borderRadius: 8,
       // backgroundImage:Globals.backgroundImage_PanelHeaderBuyDetail,
    });
    fakeWin.add(view);

	/*
    fakeWin.add(Ti.UI.createLabel({
        text: _args.title,
        color:"#fff",
        textAlign:"center",
        height: 'auto',
        top: 10,
        font:{fontSize:18,fontWeight:"bold"}
    }));
	*/
	
    _args.view.open = function() {
        win.open();
        
       
        
        
        var a1 = Titanium.UI.createAnimation();
      
        a1.curve=Titanium.UI.ANIMATION_CURVE_LINEAR;
        a1.left=0;
        a1.opacity = 1;
        a1.duration = 300;
        win.animate(a1);
    };

    _args.view.close = function() {
       
      win.hide();
    };
    
    /*
	var tableview = Ti.UI.createTableView({
			top:50,
			width:472,
			height:200,
			bottom:100,
		    backgroundColor:'transparent',
			backgroundImage:Globals.backgroundImage_TableView,
		
		});
	view.add(tableview);
	
	*/
	var lb_thongbao = Titanium.UI.createLabel({
     
       text:'Chọn bắt đầu tải để tải toàn bộ bài giảng về máy',
       textAlign:'center',
       color:'black',
       left:5,
       right:5,
       font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			//fontWeight:'bold'
		},
		visible:true
    });
    view.add(lb_thongbao)
    
    
	var lb_DangTai = Titanium.UI.createLabel({
       top:60,
       text:'',
       textAlign:'center',
       color:'black',
       left:5,
       right:5,
       font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			//fontWeight:'bold'
		},
		visible:false,
    });
    view.add(lb_DangTai)
	
	var ind=Titanium.UI.createProgressBar({
			min:0,
			max:1,
			value:0,
			style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
			top:75,
			width:490,
			height:50,
			//message:'Downloading ' + (Ti.Platform.name == 'android' ? 'PNG' : 'PDF') + ' File',
			font:{fontSize:12, fontWeight:'bold'},
			color:'#888',
			visible:false
	});
	
	view.add(ind);
	
	/*
	var lb_TongTienTrinh = Titanium.UI.createLabel({
       top:120,
       text:'Tổng tiến trình',
       textAlign:'center',
       color:'black',
       left:5,
       right:5,
       font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			//fontWeight:'bold'
		},
		visible:false,
    });
    view.add(lb_TongTienTrinh)
    */
	var ind_total=Titanium.UI.createProgressBar({
			min:0,
			max:100,
			value:0,
			style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
			top:135,
			width:490,
			height:50,
			//message:'Downloading ' + (Ti.Platform.name == 'android' ? 'PNG' : 'PDF') + ' File',
			font:{fontSize:12, fontWeight:'bold'},
			color:'#888',
			visible:false
	});
	
	view.add(ind_total);
	
		
	
    
    /*
	var lb_DaTaiXong = Titanium.UI.createLabel({
	   top:80,
       text:'Đã tải:',
       color:'Blue',
       left:5,
       right:5,
       font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			//fontWeight:'bold'
		},
		visible:false
    });
    view.add(lb_DaTaiXong)
	*/
		

	var btDownload = Titanium.UI.createButton({
		title:Globals.btBeginDownload,
		font:{
				fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
				//fontWeight:'bold'
			},
		backgroundImage: aps+Globals.backgroundImage_button,
		backgroundSelectedImage:aps+Globals.backgroundSelectedImage,
		backgroundDisabledImage: aps+Globals.backgroundDisabledImage,
		width:490/2	,
		height:45	,
		left:5,
	    bottom:5,
		color:Globals.foreColor,
	});
	
	btDownload.addEventListener('click',function()
	{
	 
		 if (Ti.Network.online)
		 {
				btDownload.title=Globals.btDownloading;
				btDownload.enabled=false;
				lb_thongbao.visible=false;
				lb_DangTai.visible=true;
		
				ind_total.show();
				ind.show();
				var path=''
				var downloadQueue = [];
				if(Codestrong.isAndroid()) path = Titanium.Filesystem.externalStorageDirectory;
				else path = Titanium.Filesystem.applicationDataDirectory;
			
				if(_args.item.IsDownloadAll==0)
				{
					//Tai 1 phan
					//alert('TAI 1 PHAN')
					//downloadfile(_args.item)
					
					for( i = 0; i < 1; i++)
					{
						var itemvideo=_args.item.dataDownload[_args.item.Index]
						var item={
							  	LectureId:_args.item.LectureId,
							  	LectureName:_args.item.LectureName,
							  	Detail:_args.item.LectureName,
							  	Price:_args.item.Price,
							  	TeacherImage:_args.item.TeacherImage,
							  	VideoID:itemvideo.Id,
							  	VideoName:itemvideo.Name,
							  	VideoFile:itemvideo.VideoFile,
							  	VideoUrl:itemvideo.VideoUrl,
						  	}
						  	
						  	downloadQueue.push({
								'filepath' : path + item.VideoFile+'.mp4',
								'url' : item.VideoUrl,
								'item':item,
								'index':i+1,
								'length':length
								
							});
					}
					require('settings').downloadMultiFile(downloadQueue, updatevalueprocess,_callBack_DownloadOneFileFinished, _callBack_DownloadMultipleFileFinished);
					
				}
				else
				{
					//tai tat ca
					//alert('Tai tat ca')
					var length=_args.item.dataDownload.length
				
					for( i = 0; i < length; i++) {
						var itemvideo=_args.item.dataDownload[i]
						var item={
							  	LectureId:_args.item.LectureId,
							  	LectureName:_args.item.LectureName,
							  	Detail:_args.item.LectureName,
							  	Price:_args.item.Price,
							  	TeacherImage:_args.item.TeacherImage,
							  	VideoID:itemvideo.Id,
							  	VideoName:itemvideo.Name,
							  	VideoFile:itemvideo.VideoFile,
							  	VideoUrl:itemvideo.VideoUrl,
						  	}
						  	
						  	downloadQueue.push({
								'filepath' : path + item.VideoFile+'.mp4',
								'url' : item.VideoUrl,
								'item':item,
								'index':i+1,
								'length':length
								
							});
					}
					require('settings').downloadMultiFile(downloadQueue, updatevalueprocess,_callBack_DownloadOneFileFinished, _callBack_DownloadMultipleFileFinished);
				}	
		 }
		 else
		 {
		  	   require('settings').createAlertConnectError();
		 }
		
	})
	var _callBack_DownloadOneFileFinished = function(download_result) {
		if( typeof (download_result) !== 'undefined') {
		
			Ti.API.info('Download susscess :' + download_result.path);
			//lb_DaTaiXong.visible=true;
			//lb_DaTaiXong.text=lb_DaTaiXong.text+','+download_result.item.VideoName
			//Download Finish
			var db = require('db');
			var todoItems = db.insertLectureDownload(download_result.item);
		//	ind_total.value= (100/download_result.length)*download_result.index;
			
		}
	};
	var _callBack_DownloadMultipleFileFinished = function() {
	    ind_total.hide();
		ind.hide();
		lb_DangTai.visible=false;
		//lb_TongTienTrinh.visible=false;
		lb_thongbao.visible=true;
		lb_thongbao.text='Đã tải xong bài giảng được chọn'
		btDownload.title='Đã tải xong. ';
		btCancel.title='Kết thúc. ';

	};
	var updatevalueprocess = function(download_result) {
		if( typeof (download_result) !== 'undefined') {
			lb_DangTai.text='Đang tải '+download_result.VideoName+'  ('+Math.round(download_result.value*100) + '%)';
			//Ti.API.info('value :' + download_result.value);
			ind.value = download_result.value ;
			
			
			//lb_TongTienTrinh.text='Tổng tiến trình ('+ (100/download_result.length)*download_result.index + '%)';
		
			//var sau=(download_result.value*100)*(100/download_result.length)*download_result.index/100;
			//	Ti.API.info('value :' + sau);
			
			ind_total.max=download_result.length
			ind_total.value= (download_result.index-1)+download_result.value
		}
	};
	
	
	
	
	downloadfile=function(item)
	{
		    // alert('TAI tat ca'+item.VideoID+'____'+item.VideoName)

			ind.value = 0;
			c = Titanium.Network.createHTTPClient();
			c.setTimeout(10000);
			c.onload = function()
			{
				
				Ti.API.info('IN ONLOAD ');
		    
		    	
		    	//Download Finish
				var db = require('db');
				var todoItems = db.insertLectureDownload(item);
			
				btDownload.title='Đã tải xong. ';
			    btCancel.title='Kết thúc. ';
		    	////////////////////////////////////////////////////////////
				var filename = item.VideoFile+'.mp4';//Titanium.Platform.name == 'android' ? settings.VideoFile+'.mp4': 'test.pdf';
			    var f;
			    if(Codestrong.isAndroid()) 
				{
					f = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory,filename);
				}
				else 	
				{
				    f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
				}
				Ti.API.info(f.nativePath);
		    
				//alert(f.nativePath)
				//if (Titanium.Platform.name == 'android') {
					f.write(this.responseData);
				//}
		
				
			
				
			};
			c.ondatastream = function(e)
			{
				ind.value = e.progress ;
				Ti.API.info('ONDATASTREAM1 - PROGRESS: ' + e.progress);
			};
			c.nonerror = function(e)
			{
				Ti.API.info('XHR Error ' + e.error);
			};
			// open the client
			if (Titanium.Platform.name == 'android') {
				//android's WebView doesn't support embedded PDF content
				c.open('GET', Globals.urlvideo);
			} else {
				c.open('GET',Globals.urlvideo);
				c.file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, item.VideoFile+'.mp4');
			}
		
			// send the data
			c.send();
	}
	
	view.add(btDownload)
	
	
	
	var btCancel = Titanium.UI.createButton({
		title:Globals.btCancel_popup,
		font:{
				fontSize:Ti.App.Properties.getString('fontsizeDefault')//,
				//fontWeight:'bold'
			},
		backgroundImage: aps+Globals.backgroundImage_button,
		backgroundSelectedImage:aps+Globals.backgroundSelectedImage,
		backgroundDisabledImage: aps+Globals.backgroundDisabledImage,
		width:490/2	,
		height:45	,
		right:5,
	    bottom:5,
		color:Globals.foreColor
	});
	btCancel.addEventListener('click',function()
	{
		
		win.close({animated:true})
	})
	view.add(btCancel)
	
    return _args.view;
};

                                                                                                                                                                                                                                                                                     